Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Text
Imports System.Windows.Forms

Namespace C4F.DevKit.Speech.Controls
	Public Partial Class SpeechSettings
		Inherits UserControl
		Public Delegate Sub SettingsAppliedEventHandler(ByVal sender As Object, ByVal e As EventArgs)
		Public Event SettingsApplied As SettingsAppliedEventHandler

		Public Delegate Sub SettingsCanceledEventHandler(ByVal sender As Object, ByVal e As EventArgs)
		Public Event SettingsCanceled As SettingsCanceledEventHandler

		#Region "Constants"

		Private Const DESIGNER_CATEGORY As String = "Speech"

		#End Region ' Constants

		#Region "Private variables"

		Private _speechVolume As Integer = 0
		Private _speechRate As Integer = 0
		Private _sapiManager As SapiManager = Nothing
		Private _voices As List(Of VoiceInfo)

		#End Region ' Private variables

		#Region "Properties"

		''' <summary>
		''' The SAPI Manager component
		''' </summary>
		<Browsable(True), Category(DESIGNER_CATEGORY)> _
		Public Property SapiManager() As SapiManager
			Get
				Return _sapiManager
			End Get
			Set(ByVal value As SapiManager)
				_sapiManager = value

				If (Not Me.DesignMode) AndAlso Not _sapiManager Is Nothing Then
					Try
						_voices = _sapiManager.Voices
						FillInstalledVoicesInComboBox()

						PopulateProperties()


					Catch
						MessageBox.Show("Unable to find installed voices, Application will use default voice!", "SpeechGUI", MessageBoxButtons.OK, MessageBoxIcon.Error)
					End Try
				ElseIf _sapiManager Is Nothing Then
					_voices = Nothing
				End If
			End Set
		End Property

		#End Region ' Properties

		#Region "Constructor"

		''' <summary>
		''' Initializes a new instance of the SpeechSettings control.
		''' </summary>
		Public Sub New()
			InitializeComponent()
		End Sub

		#End Region ' Constructor

		#Region "Methods"

		#Region "Event Handlers"

		Private Sub trackBarVolume_Scroll(ByVal sender As Object, ByVal e As EventArgs) Handles trackBarVolume.Scroll
			_speechVolume = trackBarVolume.Value
		End Sub

		Private Sub trackBarRate_Scroll(ByVal sender As Object, ByVal e As EventArgs) Handles trackBarRate.Scroll
			_speechRate = trackBarRate.Value
		End Sub

		Private Sub rdPersonName_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles rdPersonName.CheckedChanged
			If rdPersonName.Checked = True Then
				Try
					FillInstalledVoicesInComboBox()
					cmbPersons.Enabled = True
					cmbGender.Enabled = False
					cmbAge.Enabled = False
				Catch
					MessageBox.Show("Unable to find installed voices, Application will use default voice!", "SpeechGUI", MessageBoxButtons.OK, MessageBoxIcon.Error)
				End Try
			End If
		End Sub

		Private Sub rdSelectGender_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles rdSelectGender.CheckedChanged
			If rdSelectGender.Checked = True Then
				Try
					FillInstalledVoicesGendersInComboBox()
					cmbPersons.Enabled = False
					cmbGender.Enabled = True
					cmbAge.Enabled = True
				Catch
					MessageBox.Show("Unable to find installed voices, Application will use default voice!", "SpeechGUI", MessageBoxButtons.OK, MessageBoxIcon.Error)
				End Try
			End If
		End Sub

		Private Sub cmbGender_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cmbGender.SelectedIndexChanged
			FillInstalledVoicesAgeInComboBox()
		End Sub

		Private Sub btnApply_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnApply.Click
			OnApplySettings()
			OnApplied(New EventArgs())
		End Sub

		#End Region ' Event Handlers

		#Region "Private"

		Private Sub FillInstalledVoicesGendersInComboBox()
			cmbGender.Items.Clear()
			For Each v As VoiceInfo In _voices
				If (Not CheckIfAlreadyExists(cmbGender.Items, v.Gender.ToString())) Then
					cmbGender.Items.Add(v.Gender.ToString())
				End If
			Next v
			If cmbGender.Items.Count > 0 Then
				cmbGender.SelectedIndex = 0
			End If
		End Sub

		Private Sub FillInstalledVoicesAgeInComboBox()
			cmbAge.Items.Clear()
			For Each v As VoiceInfo In _voices
				If cmbGender.Text = v.Gender.ToString() Then
					If (Not CheckIfAlreadyExists(cmbAge.Items, v.Age.ToString())) Then
						cmbAge.Items.Add(v.Age.ToString())
					End If
				End If
			Next v

			If cmbAge.Items.Count > 0 Then
				cmbAge.SelectedIndex = 0
			End If
		End Sub

		Private Sub FillInstalledVoicesInComboBox()
			cmbPersons.Items.Clear()
			For Each v As VoiceInfo In _voices
				cmbPersons.Items.Add(v.Name)
			Next v
			If cmbPersons.Items.Count > 0 Then
				cmbPersons.SelectedIndex = 0
			End If
		End Sub

		Private Sub OnApplySettings()
			Me.SapiManager.VoiceRate = _speechRate
			Me.SapiManager.VoiceVolume = _speechVolume

			SelectVoice()
		End Sub

		Private Sub SelectVoice()
			If _voices.Count > 0 Then
				If rdPersonName.Checked = True Then
					Me.SapiManager.SelectVoice(cmbPersons.Text)
				End If

				If rdSelectGender.Checked = True Then
					Dim gender As VoiceGender = VoiceGender.NotSet

					If cmbGender.Text.Equals(VoiceGender.Female.ToString()) Then
						gender = VoiceGender.Female
					ElseIf cmbGender.Text.Equals(VoiceGender.Male.ToString()) Then
						gender = VoiceGender.Male
					ElseIf cmbGender.Text.Equals(VoiceGender.Neutral.ToString()) Then
						gender = VoiceGender.Neutral
					ElseIf cmbGender.Text.Equals(VoiceGender.NotSet.ToString()) Then
						gender = VoiceGender.NotSet
					End If

					Dim age As VoiceAge = VoiceAge.NotSet
					If cmbAge.Text.Equals(VoiceAge.Adult.ToString()) Then
						age = VoiceAge.Adult
					ElseIf cmbAge.Text.Equals(VoiceAge.Child.ToString()) Then
						age = VoiceAge.Child
					ElseIf cmbAge.Text.Equals(VoiceAge.NotSet.ToString()) Then
						age = VoiceAge.NotSet
					ElseIf cmbAge.Text.Equals(VoiceAge.Senior.ToString()) Then
						age = VoiceAge.Senior
					ElseIf cmbAge.Text.Equals(VoiceAge.Teen.ToString()) Then
						age = VoiceAge.Teen
					End If
					Me.SapiManager.SelectVoice(gender, age)
				End If
			End If
		End Sub

		Private Function CheckIfAlreadyExists(ByVal objectCollection As ComboBox.ObjectCollection, ByVal str As String) As Boolean
			For Each obj As Object In objectCollection
				If obj.ToString() = str Then
					Return True
				End If
			Next obj
			Return False
		End Function

		Private Sub PopulateProperties()
			Me._speechRate = Me.SapiManager.VoiceRate
			Me.trackBarRate.Value = Me._speechRate
			Me._speechVolume = Me.SapiManager.VoiceVolume
			Me.trackBarVolume.Value = Me._speechVolume

			Dim voice As VoiceInfo = SapiManager.CurrentVoice

			If (Not String.IsNullOrEmpty(voice.Name)) Then
				rdPersonName.Checked = True
				cmbPersons.SelectedItem = voice.Name
			Else
				rdSelectGender.Checked = True
				cmbGender.SelectedItem = voice.Gender.ToString()
				cmbAge.SelectedItem = voice.Age.ToString()
			End If
		End Sub

		Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCancel.Click
			OnCanceled(New EventArgs())
		End Sub

		Private Sub OnCanceled(ByVal e As EventArgs)
			If Not SettingsCanceledEvent Is Nothing Then
				RaiseEvent SettingsCanceled(Me, e)
			End If
		End Sub

		Private Sub OnApplied(ByVal e As EventArgs)
			If Not SettingsAppliedEvent Is Nothing Then
				RaiseEvent SettingsApplied(Me, e)
			End If
		End Sub

		#End Region ' Private

		#End Region ' Methods       
	End Class
End Namespace